package com.ykt.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.ykt.common.pay.wx.WxAppPayUtil;
import com.ykt.content.Content;
import com.ykt.model.Admin;

public class LoginAdminInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation ai) {
		Controller ctrl = ai.getController();

        // 微信支付则不拦截
        if(WxAppPayUtil.isPayServerCallBack(ctrl.getRequest())){
            ai.invoke();
            return ;
        }

		Admin admin = ctrl.getSessionAttr(Content.SESSION_LOGIN_USER);
        if (admin != null) {
			ai.invoke();
            return ;
		}

        // 如果是ajax请求响应头会有，x-requested-with；
        String requestedWith = ctrl.getRequest().getHeader("x-requested-with");
        if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")){
            ctrl.getResponse().setHeader("sessionstatus", "timeout");// 在响应头设置session状态
            ctrl.renderText("timeout");
        } else {
            String js="<script>alert('登录超时, 请重新登录.');window.parent.location.href='/';</script>";
            ctrl.renderText(js, "text/html");
        }
    }

}
